import cv2
import numpy as np

# 生成字符画
arr = ["@", "W", "#", "$", "O", "E", "X", "C", "[", "(", "/", "?", "=", "^", "~", "_", ".", " "]
arr_len = arr.__len__() - 1
# 读图
img = cv2.imread("images/frames/1.png", cv2.IMREAD_GRAYSCALE)

img = cv2.bitwise_not(img)
# 显示灰度图
# cv2.imshow("img", img)

# 初始化与原图一样尺寸的黑色图片
h = np.size(img, 0)
w = np.size(img, 1)
res = np.ndarray([h, w])
font = cv2.FONT_HERSHEY_SIMPLEX
i_range = range(0, h, 5)
i_range = i_range[0:-1]
j_range = range(0, w, 5)
j_range = j_range[0:-1]
for i in i_range:
    for j in j_range:

        t = arr[round((img[i,j]) / 255 * arr_len)]
        # 绘制字符
        cv2.putText(res, t, (j, i), font, 0.1, color=(255, 255, 255))
# 显示结果图
# cv2.imshow("res", res)
# 保存结果图
cv2.imwrite("res.jpg", res)

# cv2.waitKey()
# cv2.destroyAllWindows()

# np.vstack(img1, img2):在竖直方向上堆叠
# np.hstack():在水平方向上平铺堆叠

